class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution {
    //反转链表
    public ListNode reverseList(ListNode head) {
        ListNode preNode = null;
        ListNode cur = head;
        ListNode node = null;
        while (cur != null) {
            //记录cur后一个节点
            node = cur.next;
            //改变cur节点指向
            cur.next = preNode;
            //记录cur节点位置
            preNode = cur;
            //cur节点向前走
            cur = node;
        }
        //cur前一个节点为最后一个节点
        return preNode;
    }
}
